<template>
  <div class="container">
    <div class="app-container">
      <el-tree :data="department" default-expand-all :expand-on-click-node="false" :props="defaultProps">
        <template #default="{data}">
          <!-- el-row 行  -->
          <el-row type="flex" justify="space-between" style="width:100%;height:40px" align="middle">
            <!-- el-col 列 -->
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="manager-name">{{ data.managerName }}</span>
              <!-- 不传参 和 传$event 是相同的 changeDeparts($event) === changeDeparts()
              $event 占位
               -->
              <el-dropdown @command="changeDeparts($event,data.id)">
                <!-- 添加子部门 需要知道他的父部门的id  -->
                <span class="el-dropdown-link">操作<i class="el-icon-arrow-down el-icon--right" /></span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 封装 添加部门的组件 dialog  -->

    <!--  全写
    <AddDeppt :show-dialog="showDialog"
    @update:showDialog="showDialog = $event"
    /> -->
    <!-- 简写 -->
    <!-- 父传子 把 id 传递给 子组件  @updateDepartment="getDepart"  调用子组件的方法 更新部门列表
      ref="AddDepptCom" 先获取到子组件实例
      -->
    <AddDeppt ref="AddDepptCom" :depart-id="departId" :show-dialog.sync="showDialog" @updateDepartment="getDepart" />
  </div>
</template>
<script>
import { getDepartment, delDepartment } from '@/api/department'
import { getDepartmentListToTree } from '@/utils/index'
import AddDeppt from './components/AddDeppt'
export default {
  name: 'Department',
  components: {
    AddDeppt
  },
  data() {
    return {
      departId: null,
      showDialog: false,
      department: [
        {
          name: '传智教育',
          managerName: '管理员',
          children: [
            {
              name: '总裁办',
              managerName: '丽丽2'

            }, {
              name: '行政部',
              managerName: '丽丽3'

            },
            {
              name: '人事部',
              managerName: '丽丽4',
              children: [
                {
                  managerName: '丽5',
                  name: 'caiwu'
                }, {
                  managerName: '丽丽6',
                  name: '薪资'
                }
              ]
            }
          ]
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getDepart()
  },
  methods: {
    async changeDeparts(type, id) {
      if (type === 'add') {
        // 添加子部门，
        // 显示弹层，传递当前节点的id
        this.departId = id
        this.showDialog = true
        // console.log(this.departId)
      } else if (type === 'edit') {
      // 显示弹层，传递当前节点的id
        this.departId = id
        // 显示弹层
        this.showDialog = true
        // 调用子组件的获取部门详情的方法 ：
        // 通过ref-获取到子组件实例
        // 调用子组件的方法
        // 因为 vue 更新dom 的过程是异步的 所以我们不能直接得到子组件传过来的id
        // 通过$nextTick 获取更新后的dom 和 实例
        this.$nextTick(() => {
          this.$refs.AddDepptCom.getDepartmentDetail()
        })
      } else {
        await this.$confirm('你确认删除此部门吗？', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        })
        await delDepartment(id)
        this.$message.success('删除成功')
        this.getDepart()
      }
    },
    async getDepart() {
      // 获取-部门列表
      const res = await getDepartment()
      // console.log(res)
      this.department = getDepartmentListToTree(res, 0)
    }

  }
}
</script>
<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.manager-name {
  display: inline-block;
  width: 50px;
  margin-right: 10px;
}
</style>
